package gengine.common.misc;

import gengine.common.packets.PacketFactory;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: arseniy
 * Date: 24.12.11
 */
public abstract class BaseApplication {
    protected static BaseApplication singleton;
    protected PacketFactory packetFactory;
    private PrintStream writer;

    public static final boolean DEBUG = true;

    public static <T extends BaseApplication> T getApplication() {
        return (T)singleton;
    }
    protected BaseApplication(){singleton=this;}

    public void run() {
        try {
            if (DEBUG)
                writer = System.out;
            else
                writer = new PrintStream(new FileOutputStream("log.txt",true));
            writer.println(new SimpleDateFormat("dd.MM.yy H:mm:ss").format(new Date()) +  "- Application started");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void quit() {
        log("Application closed",LogCategory.Info);
        writer.close();
    }

    public PacketFactory getPacketFactory() {
        return packetFactory;
    }

    SimpleDateFormat format = new SimpleDateFormat("H:mm:ss");
    public void logInstance(String text, LogCategory category) {
        if (DEBUG || category != LogCategory.Debug)    {}
          writer.println(format.format(new Date()) + " - "+text);
    }
    
    public static void log(String text,LogCategory category) {
        getApplication().logInstance(text,category);
    }
}
